package com.fulihui.wgj.schedule.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.request.rpc.AppRequest;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.util.DateUtils;
import com.fulihui.common.util.JedisUtil;
import com.fulihui.wgj.facade.dto.WxParamQrcodeInfoDTO;
import com.fulihui.wgj.facade.dto.WxPlatformDTO;
import com.fulihui.wgj.facade.request.qrcode.WxQrcodeEventStatSqlRequest;
import com.fulihui.wgj.facade.service.platform.WxPlatformService;
import com.fulihui.wgj.facade.service.qrcode.WxParamQrcodeService;
import com.fulihui.wgj.schedule.annotation.MutexLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author: LiuJie
 * @version: 2018/7/7 10:21
 * @description:
 */
@Component
@Slf4j
public class WxQrcodeEventStatJob {
    @Reference(version = "1.0.0")
    WxPlatformService wxPlatformService;
    @Reference(version = "1.0.0")
    WxParamQrcodeService wxQrcodeEventStatService;
    @Autowired
    JedisUtil jedisUtil;

    @MutexLock(node = "${job.node.wxQrcodeEventStatJob_execute}")
    @Scheduled(cron = "${job.cron.wxQrcodeEventStatJob_execute}")
    public void excute() throws InterruptedException {

        log.info("Sync qrcode stat data job is start!");
        // 获取所有有效公众号
        List<WxPlatformDTO> platformList = wxPlatformService.queryUsefulPlatform().getData();
        if (CollectionUtils.isEmpty(platformList)) {
            log.info("platform list is empty, job over！");
            return;
        }
        // 循环每个公众号
        for (WxPlatformDTO platformDTO : platformList) {
            String appId = platformDTO.getAppId();
            // 查询该公众号的所有参数二维码
            AppRequest request = new AppRequest();
            request.setAppId(appId);
            List<WxParamQrcodeInfoDTO> data = wxQrcodeEventStatService.queryParamQrcodeInfoByAppId(request).getData();
            if (CollectionUtils.isEmpty(data)) {
                log.info("qr_scene is empty! appid = {}", appId);
                continue;
            }
            // 循环请求统计接口
            for (WxParamQrcodeInfoDTO paramQrcodeInfoDTO : data) {
                WxQrcodeEventStatSqlRequest statSqlRequest = new WxQrcodeEventStatSqlRequest();
                statSqlRequest.setAppId(appId);
                statSqlRequest.setSceneId(paramQrcodeInfoDTO.getSceneStr());
                statSqlRequest.setGmtAttention(DateUtils.formatWebFormat(DateUtils.addDays(new Date(), -1)));
                BaseResult baseResult = wxQrcodeEventStatService.saveStatBySql(statSqlRequest);
                log.info("参数二维码统计,request = {}, result = {}", statSqlRequest, baseResult);
                Thread.sleep(2000L);
            }
        }

        log.info("Sync qrcode stat data job is terminate!");

    }


}
